//
//  NSString+Extention.m
//  TotalDemo
//
//  Created by gujian on 16/2/14.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "NSString+Extention.h"

@implementation NSString (Extention)

+ (BOOL)isIdentityCard:(NSString *)str
{
    if (str.length <= 0) return NO;
    
    NSString *regex = @"^\\d{15}|\\d{18}|\\d{17}[\\d|x|X|]{1}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [pred evaluateWithObject:str] && [self strongCheckIdentityCard:str];
}

/** 强验证身份证号 */
+ (BOOL)strongCheckIdentityCard:(NSString *)str
{
    int identifyCard[17];
    int weight[17] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
    char checkMask[11] = {'1', '0', 'X','9','8','7','6','5','4','3','2'};
    char identifyNum = '0';
    for (int i = 0; i < 17; i++) {
        identifyNum = [str characterAtIndex:i];
        identifyCard[i] = identifyNum - '0';
    }
    
    int sum = 0;
    
    for (int i = 0; i < 17; i++) {
        sum += weight[i] * identifyCard[i];
    }
    
    int result = sum%11;
    char realCheckBit = checkMask[result];
    char checkBit = [str characterAtIndex:17];
    
    return realCheckBit==checkBit;
}

+ (NSString *)prodeceIndetityCard
{
    NSString *identityStr = @"";
    
    //先生成地区（1-6），再生成年、月、日（7-14），派出所代码(15-16)，性别(17,奇男偶女)，校验码(18)
    
    
    return identityStr;
}


+ (BOOL)isChineseCharacter:(NSString *)str
{
    __block BOOL result = NO;
    NSRange range = NSMakeRange( 0, str.length);
    
//    //方法一
    
//
//    NSRegularExpression *rex = [NSRegularExpression regularExpressionWithPattern:@"\\p{script=Han}" options:NSRegularExpressionCaseInsensitive error:nil];
//    NSInteger hanNum = [rex numberOfMatchesInString:str options:0 range:range];
//    if (hanNum == [str length]) {
//        result = YES;
//    }
    
//    //方法二
//    [str enumerateSubstringsInRange:range options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
//        
//        const char *cStr = [substring UTF8String];
//         result = YES;
//        if (strlen(cStr) != 3) {
//            result = NO;
//            *stop = YES;
//        }
//        
//       
//    }];
//
    
    //方法三
    for (int i = 0; i < str.length; i++) {
        int a = [str characterAtIndex:i];
        if (a > 0x4e00 && a < 0x9fff) {
            
        }else
        {
            result = NO;
            break;
        }
            
        result = YES;
    }
    
    return result;
}

@end





















